package com.controller;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.SeatExcel;
import com.bean.SeatLog;
import com.service.SeatLogService;
import com.utils.Query;
import com.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController()
@RequestMapping("/seatLog")
public class SeatLogController {

    @Autowired
    SeatLogService seatLogService;

    @RequestMapping("/list")
    public R list(@RequestBody Query query) {
        IPage<SeatLog> page = seatLogService.queryByPage(query);
        return R.ok().put("meta", page);
    }


    @GetMapping("download")
    public void download(SeatLog queryParams, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("选座记录", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        LambdaQueryWrapper<SeatLog> seatLogLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (queryParams != null) {
            if (StringUtils.isNotEmpty(queryParams.getSeatName())) {
                seatLogLambdaQueryWrapper = seatLogLambdaQueryWrapper.eq(SeatLog::getSeatName, queryParams.getSeatName().trim());
            }
            if (StringUtils.isNotEmpty(queryParams.getUserName())) {
                seatLogLambdaQueryWrapper = seatLogLambdaQueryWrapper.eq(SeatLog::getUserName, queryParams.getUserName().trim());
            }
            if (StringUtils.isNotEmpty(queryParams.getIp())) {
                seatLogLambdaQueryWrapper = seatLogLambdaQueryWrapper.eq(SeatLog::getIp, queryParams.getIp().trim());
            }
            if (queryParams.getBeginTime() != null) {
                seatLogLambdaQueryWrapper = seatLogLambdaQueryWrapper.ge(SeatLog::getBeginTime, queryParams.getBeginTime());
            }
            if (queryParams.getEndTime() != null) {
                seatLogLambdaQueryWrapper = seatLogLambdaQueryWrapper.le(SeatLog::getEndTime, queryParams.getEndTime());
            }
        }
        List<SeatLog> list = seatLogService.list(seatLogLambdaQueryWrapper);
        List<SeatExcel> seatExcelList = new ArrayList<SeatExcel>();
        for (int i = 0; i < list.size(); i++) {
            SeatExcel seatExcel = new SeatExcel();
            BeanUtils.copyProperties(list.get(i), seatExcel);
            seatExcel.setNum(i + 1L);
            seatExcelList.add(seatExcel);
        }
        EasyExcel.write(response.getOutputStream(), SeatExcel.class).sheet("sheet1").doWrite(seatExcelList);
    }
}
